/**
* @program: src
*
* @description:json类型
*
* @author: Mr.chen
*
* @create: 2020-11-04 17:35
**/
package jsonp

import (
	"ydd-im/util/response"
	"errors"
	"github.com/gin-gonic/gin"

)
// 中间件-跨域请求r.Use(CorsMiddleware())
func SetUp() gin.HandlerFunc {
	return func(c *gin.Context) {
		err := verifyCorsMiddleware(c)
		if err != nil {
			response.ResponseJsonFail(c,response.Response{
				Code:response.ErrorCode,
				Message:err.Error(),
			})
			c.Abort()
			return
		}
		c.Next()
	}
}
func verifyCorsMiddleware(c *gin.Context)  error {
	method := c.Request.Method
	var openCorsFlag = true
	if openCorsFlag {
		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
		c.Header("Access-Control-Allow-Methods", "GET, OPTIONS, POST, PUT, DELETE")

		//c.Set("content-type", "application/json")
	}
	if method == "OPTIONS" {
		return errors.New( "访问类型有误")
		//c.JSON(http.StatusOK, nil)
	}
	return nil

}

